﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseUi
{
    /// <summary>
    /// 本地文件
    /// </summary>
    public class LocalFile
    {
        /// <summary>
        /// 程序运行名称（BaseUi.exe）
        /// </summary>
        public static readonly string AppName = AppDomain.CurrentDomain.FriendlyName.Contains('.') ? AppDomain.CurrentDomain.FriendlyName : $"{AppDomain.CurrentDomain.FriendlyName}.exe";//多环境兼容性
        /// <summary>
        /// 程序运行目录
        /// </summary>
        public static readonly string AppDir = AppDomain.CurrentDomain.BaseDirectory;
        /// <summary>
        /// 数据目录
        /// </summary>
        public static readonly string DataDir = Path.Combine(AppDir, "data");
        /// <summary>
        /// 日志目录
        /// </summary>
        public static readonly string LogDir = Path.Combine(AppDir, "logs");

        /// <summary>
        /// 运行主程序
        /// </summary>
        public static readonly string AppPath = Path.Combine(AppDir, AppName);
        /// <summary>
        /// 配置文件路径
        /// </summary>
        public static readonly string ConfigPath = Path.Combine(DataDir, "jsconfig.json");
        /// <summary>
        /// 设备手动点位文档路径
        /// </summary>
        public static readonly string PlcDotPath = Path.Combine(DataDir, "设备手动点位.xlsx");
        /// <summary>
        /// 帮助文档路径
        /// </summary>
        public static readonly string DocPath = Path.Combine(DataDir, "操作说明书.docx");

        static object lockConfig = new object();
        static JsConfig config;
        /// <summary>
        /// 配置信息
        /// </summary>
        public static JsConfig Config
        {
            get
            {
                if (config != null)
                    return config;
                else
                {
                    try
                    {
                        if (File.Exists(ConfigPath))
                            lock (lockConfig)
                                config = JsonConvert.DeserializeObject<JsConfig>(File.ReadAllText(ConfigPath, Encoding.UTF8));
                        else
                            config = null;
                    }
                    catch (Exception)
                    {
                        config = null;
                    }
                }
                return config ?? new JsConfig();
            }
        }

        /// <summary>
        /// 刷新配置信息。将本地的配置信息重新加载到内存中
        /// </summary>
        public static void UpdateConfig()
        {
            if (File.Exists(ConfigPath))
                lock (lockConfig)
                    config = JsonConvert.DeserializeObject<JsConfig>(File.ReadAllText(ConfigPath, Encoding.UTF8));
            else
                config = null;
        }

        /// <summary>
        /// 保存配置信息。将内存中的配置信息保存到本地
        /// </summary>
        public static void SaveConfig()
        {
            try
            {
                lock (lockConfig)
                    File.WriteAllText(ConfigPath, JsonConvert.SerializeObject(Config, Formatting.Indented), Encoding.UTF8);
            }
            catch (Exception ex)
            {
                UpdateConfig();
                throw ex;
            }
        }
    }
}
